package com.xzmzhp.controller.finance;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.finance.FinanceStatistics;
import com.xzmzhp.service.finance.FinanceStatisticsService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author: 王源(xm)
 * @create: 2021-11-06 15:40
 * @program: ams
 * @description: 财务统计信息前台交互类
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/financeStatistics")
public class FinanceStatisticsController {

    @Autowired
    private FinanceStatisticsService financeStatisticsService;

    /**
     * 查询所有财务统计信息
     *
     * @return 财务统计信息
     */
    @GetMapping("/findAll")
    public ReturnData findAll(@RequestParam(value = "page", defaultValue = "1") int page,
                       @RequestParam(value = "limit", defaultValue = "10") int limit){
        //开启分页
        PageHelper.startPage(page, limit, true);
        //查询数据
        List<FinanceStatistics> list = financeStatisticsService.findAll();
        if (StringUtils.isNotEmpty(list)){
            //返回查询信息
            PageInfo<FinanceStatistics> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(pageInfo);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询失败，请重试");
    }

    /**
     * 查询一段时间内的财务统计信息
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 一段时间内的财务统计信息
     */
    @GetMapping("/countFinanceStatistics")
    public ReturnData countFinanceStatistics(@RequestParam(value = "page", defaultValue = "1") int page,
                                      @RequestParam(value = "limit", defaultValue = "10") int limit,
                                      @RequestParam(value = "startDate") Date startDate,
                                      @RequestParam(value = "endDate") Date endDate){
        //开启分页
        PageHelper.startPage(page, limit, true);
        //查询数据
        List<FinanceStatistics> list = financeStatisticsService.countFinanceStatistics(startDate,endDate);
        if (StringUtils.isNotEmpty(list)){
            //返回查询信息
            PageInfo<FinanceStatistics> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(pageInfo);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询失败，请重试");
    }


    /**
     * 根据时间类型查询财务信息
     * @param dateType 时间类型
     * @return 月、年财务信息
     */
    @GetMapping("/findFinanceStatisticsByDateType")
    public ReturnData findFinanceStatisticsByDateType(@RequestParam(value = "dateType") Integer dateType){
        List<FinanceStatistics> list = financeStatisticsService.findFinanceStatisticsByDateType(dateType);
        if (StringUtils.isNotEmpty(list)){
            //返回查询信息
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询失败，请重试");
    }
}
